package org.platform.processor.monitor.data;

import lombok.Data;
import org.platform.mo.bussmo.diagnose.event.*;

import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 该类主要存放所有监测点的缓存资源(单例)
 *
 * @author FengJie
 * @date 2018/5/15
 */

@Data
public class CacheLibraryPO {

    private static Integer initialCapacity = 1024;

    private static CacheLibraryPO cacheLibraryPO;
    /**
     * 本集合用于存放[标准参照属性源],从数据库中加载,由{@link InitDataSource} 注入
     * 存放格式(k,v) (Deviceid_Attrid,StandardEventBO)
     */
    private Map<String, List<StandardEventBO>> standardReferenceSourceMap;

    /**
     * 本队列用于接收通过zeroMQ发送过来的所有[设备信息]
     */
    private Queue<DeviceBO> deviceQueue;

    /**
     * 本队列用于存储拆解deviceQueue 之后的单个[属性值]
     */
    private Queue<DevicePropertyBO> devicePropertyQueue;

    /**
     * 本对列存放有[异常]的设备
     */
    private Map<String, AbnormalEventBO> abnormalDeviceMap;

    /**
     * 本队列存放最终所有[报警]的设备信息 (级别  异常<报警 )
     */
    private Map<String, AlarmBO> alarmDeviceMap;

    /**
     * 本集合用于存放[报警]设备的的最新属性值,判断报警的设备是否恢复正常
     */
    private Map<String, DevicePropertyBO> latestAlarmDevicePropertyMap;

    /**
     * 获取单例对象
     *
     * @return 单例缓存库
     */
    public static CacheLibraryPO getCacheLibraryPO() {
        if (null == cacheLibraryPO) {
            synchronized (CacheLibraryPO.class) {
                cacheLibraryPO = new CacheLibraryPO();
                return cacheLibraryPO;
            }
        } else {
            return cacheLibraryPO;
        }
    }

    /**
     * 私有化构造方法
     */
    private CacheLibraryPO() {
        initQueue();
    }

    /**
     * 初始化队列
     */
    void initQueue() {
        deviceQueue = new ConcurrentLinkedQueue<>();
        devicePropertyQueue = new ConcurrentLinkedQueue<>();
        abnormalDeviceMap = new ConcurrentHashMap<>(initialCapacity);
        standardReferenceSourceMap = new ConcurrentHashMap<>(initialCapacity);
        alarmDeviceMap = new ConcurrentHashMap<>(initialCapacity);
        latestAlarmDevicePropertyMap = new ConcurrentHashMap<>(initialCapacity);
    }
}
